package com.cg.service.impl;

import com.cg.dao.RoleMapper;
import com.cg.entity.Source;
import com.cg.dao.SourceMapper;
import com.cg.service.SourceService;

import com.cg.vo.MenuAllVo;
import com.cg.vo.RoleVo;
import com.cg.vo.SourceForRoleVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;

/**
 * (Source)表服务实现类
 *
 * @author chengong
 * @since 2023-12-04 09:24:44
 */
@Service("sourceService")
public class SourceServiceImpl implements SourceService {
    @Resource
    private SourceMapper sourceMapper;

    @Resource
    private RoleMapper roleMapper;


    @Override
    public Integer createSource(Source source) {
        return sourceMapper.createSource(source);
    }

    @Override
    public SourceForRoleVo querySourceBySourceId(Integer roleId) {
        //获取角色信息
        RoleVo roleVo = roleMapper.queryRoleByRoleId(roleId);
        if(roleVo == null){
            //没有此角色
            return null;
        }
        //获取所有的权限
        List<Source> sources = sourceMapper.queryAll();
        //根据roleId获取对应的权限
        List<Source> sourceList = sourceMapper.querySourceBySourceId(roleId);

        SourceForRoleVo sourceForRoleVo = new SourceForRoleVo();
        BeanUtils.copyProperties(roleVo,sourceForRoleVo);
        sourceForRoleVo.setAllSourceList(sources);
        sourceForRoleVo.setSourceList(sourceList);

        return sourceForRoleVo;
    }

    /**
     * 加载所有的以及二级菜单权限
     */
    @Override
    public List<MenuAllVo> queryMenuAll() {

        return sourceMapper.queryMenuAll();
    }

    /**
     * 验证权限是否存在
     * @param url
     * @return
     */
    @Override
    public Integer checkSourceUrl(String url) {
        return sourceMapper.checkSourceUrl(url);
    }

    /**
     * 删除权限
     */
    @Override
    public Integer removeSource(Integer sourceId) {
        return sourceMapper.removeSource(sourceId);
    }

    /**
     * 修改权限的名称
     * @param source
     * @return
     */
    @Override
    public Integer updateSourceName(Source source) {
        Integer count = 0;
        try {
            count = sourceMapper.updateSourceName(source);
        }catch (Exception e){
            e.printStackTrace();
        }
        return count;
    }
}
